#include "stdafx.h"
#include "OgreInit.h"

template<> OgreInit* Singleton<OgreInit>::ms_Singleton = 0;
OgreInit* OgreInit::getSingletonPtr(void)
{
    return ms_Singleton;
}

OgreInit& OgreInit::getSingleton(void)
{
    assert( ms_Singleton );  return ( *ms_Singleton );
}

OgreInit::OgreInit():mRoot(0),
					 mSceneMgr(0),
					 mFirstRW(0)
{

}

OgreInit::~OgreInit()
{
	delete mRoot;
	//mRoot->shutdown();
}

SceneManager* OgreInit::getSceneManager()
{
	return mSceneMgr;
}

bool OgreInit::Init()
{	
#ifdef _DEBUG
	mResourcesCfg = "resources_d.cfg";
	mPluginsCfg = "plugins_d.cfg";
	mConfigCfg = "ogre_d.cfg";
	mLogCfg = "ogre.log";
#else
	mResourcesCfg = "resources.cfg";
	mPluginsCfg = "plugins.cfg";
	mConfigCfg = "ogre.cfg";
	mLogCfg = "ogre.log";
#endif

	Ogre::Root*mRoot = OGRE_NEW Ogre::Root(mPluginsCfg,mConfigCfg,mLogCfg);
	
	if(!mRoot->restoreConfig() && !mRoot->showConfigDialog())
	{
		return false;
	}
	mRoot->initialise(false);

	//we need to create a useless first renderwindow in order for resources to be parsed
	System::Windows::Forms::Control c;
	HWND hWnd = (HWND) (void*) c.Handle.ToPointer();
	NameValuePairList misc;
	misc["externalWindowHandle"] = Ogre::StringConverter::toString((int)hWnd);
	RenderWindow* rw = Ogre::Root::getSingletonPtr()->createRenderWindow("Firstrw", 800, 600, false, &misc);
	rw->destroy();
	
	setupResources();
	initResources();

	mSceneMgr = mRoot->createSceneManager(SceneType::ST_GENERIC);


	//mSceneMgr->setSkyBox(true,"Examples/MorningCubeMap");

ParticleSystem::setDefaultNonVisibleUpdateTimeout(5);  // set nonvisible timeout

// create some nice fireworks and place it at the origin
//ps = mSceneMgr->createParticleSystem("Fireworks", "Examples/Fireworks");
/*ps->getNumAffectors();
ps->getNumEmitters();
ps->getNumParticles();*/
//::getSingletonPtr()->
//mSceneMgr->getRootSceneNode()->attachObject(ps);

// create a green nimbus around the ogre head
ps = mSceneMgr->createParticleSystem("Nimbus", "Examples/GreenyNimbus");
mSceneMgr->getRootSceneNode()->attachObject(ps);
       

	return true;
}

void OgreInit::setupResources()
{
	//Uses Ogre's ConfigFile class to parse all of the resource from resource.cfg
	Ogre::String secName,typeName,archName;
	ConfigFile cf;
	cf.load(mResourcesCfg);
	//looping through the parsed config file
	ConfigFile::SectionIterator seci=cf.getSectionIterator();
	while(seci.hasMoreElements())
	{
		//For each section, we loop again, getting all the contents out of it
		secName = seci.peekNextKey();
		ConfigFile::SettingsMultiMap *settings = seci.getNext();
		ConfigFile::SettingsMultiMap::iterator i;
		for(i = settings->begin();i!=settings->end();++i)
		{
			//add the section name (whi	ch is the group of the resources), the type of resource
			//(zip, folder, etc), and finally the filename of the resource itself to the
			//ResourceGroupManager.
			typeName = i->first;
			archName=i->second;
			ResourceGroupManager::getSingletonPtr()->addResourceLocation(archName,typeName,secName);
		}
	}	
}

void OgreInit::initResources()
{
	// Set default mipmap level (NB some APIs ignore this)
	Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
	// initialise all resource groups
	Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}